﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.BussLogic;
using System.Data;
using System.Text;
using System.Configuration;

namespace TimKen.Web
{
    public partial class News : TimKen.App.WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        public void BindData()
        {
            int SortOrder = Convert.ToInt32(ConfigurationManager.AppSettings["SortOrder"]);//置顶的初始值

            ArticleManager am = new ArticleManager();
            DataSet ds = am.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, " TypeID=1 AND " + LanguageWhereStr);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);
            StringBuilder sb = new StringBuilder();
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0] as DataTable;
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        TimeSpan TmpTm = (DateTime.Now - Convert.ToDateTime(dr["CreateDate"]));
                        string url = "NewsDetail.aspx?ArticleID=" + Convert.ToString(dr["id"]);
                        //是否置顶
                        string StrOrder = Convert.ToInt32(dr["SortOrder"]).Equals(SortOrder) ? "<img src=\"Common/Image/iconbtnbg/ding_icon.gif\" />" : "";
                        //内容描述 截取
                        string Content = checkStr(dr["Content"].ToString());
                        Content = Content.Replace("&nbsp;", "");
                        Content = GetSubString(Content, 80, 160, LanguageID);
                        //标签 截取
                        string Title = Convert.ToString(dr["Title"]);
                        Title = GetSubString(Title, 20, 40, LanguageID);

                        sb.AppendFormat("<dl><dt><label>{0}</label><a href=\"{1}\">{2}{3}{4}</a></dt>",
                            Convert.ToDateTime(dr["CreateDate"]).ToString("yyyy年MM月dd日"), url,
                            StrOrder,
                            Title,
                            TmpTm.Days <= 14 ? "<span>New</span>" : "");

                        sb.AppendFormat("<dd>{0}<a href=\"{1}\">{2}</a></dd></dl>",
                            Content,
                            url, GetLangString("More") + "&gt;&gt;");
                    }
                }
            }
            LstNewsControl.Text = sb.ToString();
        }
    }
}